home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DJLSR106.ARJ / LDIV.C < prev    next >
C/C++ Source or Header  |  1992-03-02  |  2KB  |  60 lines

  1. /* This file may have been modified by DJ Delorie (Jan 1991).  If so,
  2. ** these modifications are Coyright (C) 1991 DJ Delorie, 24 Kirsten Ave,
  3. ** Rochester NH, 03867-2954, USA.
  4. */
  5.  
  6. /*
  7.  * Copyright (c) 1990 Regents of the University of California.
  8.  * All rights reserved.
  9.  *
  10.  * This code is derived from software contributed to Berkeley by
  11.  * Chris Torek.
  12.  *
  13.  * Redistribution and use in source and binary forms are permitted
  14.  * provided that: (1) source distributions retain this entire copyright
  15.  * notice and comment, and (2) distributions including binaries display
  16.  * the following acknowledgement:  ``This product includes software
  17.  * developed by the University of California, Berkeley and its contributors''
  18.  * in the documentation or other materials provided with the distribution
  19.  * and in all advertising materials mentioning features or use of this
  20.  * software. Neither the name of the University nor the names of its
  21.  * contributors may be used to endorse or promote products derived
  22.  * from this software without specific prior written permission.
  23.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  24.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  25.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  26.  */
  27.  
  28. #if defined(LIBC_SCCS) && !defined(lint)
  29. static char sccsid[] = "@(#)ldiv.c    5.1 (Berkeley) 5/16/90";
  30. #endif /* LIBC_SCCS and not lint */
  31.  
  32. #include <stdlib.h>        /* ldiv_t */
  33.  
  34. /*
  35.  * I AM NOT SURE THIS IS COMPLETELY PORTABLE
  36.  * (or that it is even right)
  37.  */
  38. ldiv_t
  39. ldiv(num, denom)
  40.     long num, denom;
  41. {
  42.     ldiv_t r;
  43.  
  44.     /* see div.c for comments */
  45.  
  46.     if (num > 0 && denom < 0) {
  47.         num = -num;
  48.         denom = -denom;
  49.     }
  50.     r.quot = num / denom;
  51.     r.rem = num % denom;
  52.     if (num < 0 && denom > 0) {
  53.         if (r.rem > 0) {
  54.             r.quot++;
  55.             r.rem -= denom;
  56.         }
  57.     }
  58.     return (r);
  59. }
  60.